package com.dll.pybig.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dll.pybig.dao.PathMapper;
import com.dll.pybig.dao.StuScoreMapper;
import com.dll.pybig.pojo.Path;
import com.dll.pybig.pojo.StuCommentInfo;
import com.dll.pybig.pojo.StuScore;
import com.dll.pybig.utils.ExcelUtil;
import com.dll.pybig.utils.PyRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ScoreService {

    @Autowired
    StuScoreMapper stuScoreMapper;
    @Autowired
    PathMapper pathMapper;

    @Autowired
    StuCommentService stuCommentService;

    public void insertScoreData(){
        List<StuScore> scoreList = new ArrayList<>();

        Path path = pathMapper.selectById(1l);
        String output = path.getOutput();
        File file = new File(output + "out学生总成绩.xlsx");
        List<Map<Integer, Object>> maps = ExcelUtil.readData(file);
        for (int i =1;i<maps.size() ;i++) {
            Map<Integer, Object> scoreMap = maps.get(i);
            StuScore stuScore = new StuScore();
            String ver1 = (String)scoreMap.get(1);
            String ver3 = (String)scoreMap.get(3);
            String ver4 = (String)scoreMap.get(4);
            String ver5 = (String)scoreMap.get(5);
            String ver6 = (String)scoreMap.get(6);
            stuScore.setStuNum(Long.valueOf(ver1));
            stuScore.setStuName((String)scoreMap.get(2));
            stuScore.setSignupTimes(Long.valueOf(ver3));
            stuScore.setAvgSeatNum(Double.valueOf(ver4));
            stuScore.setSignUpScore(Double.valueOf(ver5));
            stuScore.setAskScore(Double.valueOf(ver6));
            stuScoreMapper.insert(stuScore);
            scoreList.add(stuScore);
        }

    }
    public void updateData(List<StuScore> stuScores){
        for (StuScore stuScore : stuScores) {
            UpdateWrapper<StuScore> s = new UpdateWrapper<>();
            s.eq("stuNum",stuScore.getStuNum());
            stuScoreMapper.UpdateUser(stuScore,s);
        }
    }

    public List<StuScore> getAllStuScore( String names,Integer pages){
        Page<StuScore> page = new Page<>(pages,5);
        QueryWrapper<StuScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("stuName",names);
        IPage<StuScore> stuScoreIPage = stuScoreMapper.selectPage(page,queryWrapper );
        return stuScoreIPage.getRecords();
    }

    public IPage<StuScore> getAllStuScoreIpa(String names,Integer pages){

        Page<StuScore> page = new Page<>(pages,5);
        QueryWrapper<StuScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("stuName",names);

        return stuScoreMapper.selectPage(page,queryWrapper );
    }

    public Integer insertStuScore(StuScore stuScore){
        int insert = stuScoreMapper.insert(stuScore);
        return insert;
    }

    public  StuScore getStuScoreByStuNums(Long nums){
        QueryWrapper<StuScore> qw = new QueryWrapper<>();
        qw.eq("stuNum",nums);

        return stuScoreMapper.selectOne(qw);
    }


    public Integer updateByStuNums(StuScore stuScore,Long nums){

        UpdateWrapper<StuScore> up = new UpdateWrapper<>();
        up.eq("stuNum",nums);
        int update = stuScoreMapper.update(stuScore, up);
        return update;
    }


    public Integer deletedByStuNums(Long nums){
        UpdateWrapper<StuScore> de = new UpdateWrapper<>();
        de.eq("stuNum",nums);
        int delete = stuScoreMapper.delete(de);
        return delete;
    }


    /**
     *
     * @param names 搜索信息
     * @param pages 页码
     * @return 分页的值
     */
    public Object selectDeleted(String names,Integer pages){

        Page<StuScore> page = new Page<>(pages,5);
        QueryWrapper<StuScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("stuName",names);
//        queryWrapper.eq("deleted",1);
        return stuScoreMapper.selectDeleted(page,queryWrapper);
    }


    public Integer recoverUser(Long stuNum){
      return   stuScoreMapper.recoverUser(stuNum);
    }


    public Integer getDataReloaded(){

        long a= System.currentTimeMillis();//获取当前系统时间(毫秒)
        Path path = pathMapper.selectById(1l);
        String resource = path.getResource();
        int answer = 1;
        try{
            PyRunner.invokePy(new String[]{"ipython",resource+"数据分析.ipynb"});
            System.out.println("脚本执行中");
        }
        catch (Exception e){
            answer = 0;
            e.printStackTrace();
        }
        System.out.println("脚本执行完毕");
        String output = path.getOutput();
        List<Map<Integer, Object>> maps = ExcelUtil.readData(new File(output+"out学生总成绩.xlsx"));
        List<StuScore> stuScores = new ArrayList<>();
        for (int i = 1; i < maps.size(); i++) {
            Map<Integer, Object> scoreMap = maps.get(i);
            StuScore stuScore = new StuScore();
            String ver1 = (String)scoreMap.get(2);
            System.out.println("v "+ ver1);
            String ver3 = (String)scoreMap.get(4);
            String ver4 = (String)scoreMap.get(5);
            String ver5 = (String)scoreMap.get(6);
            String ver6 = (String)scoreMap.get(7);
            String ver7 = (String)scoreMap.get(8);

            stuScore.setStuNum(Long.valueOf(ver1));
            stuScore.setStuName((String)scoreMap.get(3));
            stuScore.setSignupTimes(Long.valueOf(ver3));
            stuScore.setAvgSeatNum(Double.valueOf(ver4));
            stuScore.setSignUpScore(Double.valueOf(ver5));
            stuScore.setAskScore(Double.valueOf(ver6));
            stuScore.setTotalScore(Double.valueOf(ver7));
            stuScores.add(stuScore);
        }
        updateData(stuScores);
        PyRunner.invokePy(new String[]{"python",resource+"formatData1.py"});
         List<StuCommentInfo> stuCommentInfos = stuCommentService.readCsv();
        stuCommentService.updateData(stuCommentInfos);
        System.out.println("执行更新完成");
        System.out.println(System.currentTimeMillis()-a+"毫秒");
        return answer;
    }

}
